/**
 * Copyright (c) 2019-2021,  陈俊兵 (cjb6b.com@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ktjiaoyu.advance.locktype;

import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @packageName com.ktjiaoyu.advance.locktype
 * @fileName LockReentrantTest.java
 * @author 陈俊兵 (cjb6b.com@gmail.com)
 * @createTime 2023/7/25 17:27
 * @lastModify 2023/7/25 17:27
 * @version 1.0.0
 */
public class LockReentrantTest {

    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        new Thread(()->{
            String name = Thread.currentThread().getName();
            // 第一次拿锁
            try {
                lock.lock();
                System.out.println(name + "：拿到锁，进了第一道门");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    lock.lock();
                    System.out.println(name + "：哟吼，还有一道门？还好是同一把锁，照样开~");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    wc();
                }finally {
                    lock.unlock();
                    System.out.println(name + "：出第"+(lock.getHoldCount() + 1)+"道门");
                }
            }finally {
                lock.unlock();
                System.out.println(name + "：出第"+(lock.getHoldCount() + 1)+"道门");
            }

        }, "张三").start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{

            String name = Thread.currentThread().getName();
            // 第一次拿锁
            try{
                lock.lock();
                // 礼让一下张三，先让他出来
                Thread.yield();

                System.out.println(name +"：终于进来了，差点憋死");
            }finally {
                lock.unlock();
            }
        }, "李四").start();
    }

    private static void wc() {
        String name = Thread.currentThread().getName();
        try {
            lock.lock();
            System.out.println(name + "：三道门？俄罗斯套娃呢！终于可以如厕了~");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }finally {
            lock.unlock();
            System.out.println(name + "：出第"+(lock.getHoldCount() + 1)+"道门");
        }
    }
}
